<?php

class NotiAction extends Action{
	
	
	function consume(){
		
// 		$id = $_GET['id'];
		//     	$id = '0000113158';
		$sql = " select b.SaleDate as 消费时间, a.SrcDescript as 消费类型, SrcType, srcID,
 (select SUM(Fmoney) from 
  (select mdid, ISNULL(aa.Cash, 0.00) + ISNULL(aa.bank, 0.00) + ISNULL(aa.UseCard, 0.00) as Fmoney, aa.Quantity
   from SS_ConsumeProjectDetail aa 
   left join bd_CardDetail bb on aa.CardDetailId = bb.Id
   left join bd_CardClass cc on cc.Id = bb.CardClassId 
   where aa.MDID = a.srcid and SUBSTRING(CardKindNo, 1, 3) <> '002'
   union 
   select mdid, ISNULL(Cash, 0.00) + ISNULL(bank, 0.00) + ISNULL(UseCard, 0.00) as Fmoney, 0 from SS_ConsumeGoodsDetail bb where bb.MDID = a.srcid
  )aa
 ) as 消费金额,  
 (select SUM(Quantity) from
  (select Quantity from SS_ConsumeProjectDetail aa where aa.MDID = a.srcid   
  )aa
 ) as 卡扣次数, '' as 备注, c.Name as 会员姓名, d.Shop as 门店,c.wxOpenID, c.wxCode, a.id
 from wx_SendConsumeRecord a 
 left join SS_Consume b on a.srcid = b.MDID
 left join bd_Customers c on c.ID = b.CustomerId
 left join bd_ShopSet d on d.id = b.ShopId
 where SrcType = '0' and SendState = '0' and isnull(c.wxOpenID, '') <> ''
 union all  
 select b.RegisterDate as 消费时间, a.SrcDescript as 消费类型, SrcType,srcID,
 ISNULL(Cash, 0.00) + ISNULL(b.Bank, 0.00) + ISNULL(UseCard, 0.00) + ISNULL(CardChange, 0.00) as 消费金额,  
 0 as 卡扣次数, b.Remarks as 备注, c.Name as 会员姓名, d.Shop as 门店,c.wxOpenID, c.wxCode, a.id
 from wx_SendConsumeRecord a 
 left join bd_CardDetail b on a.srcid = b.Id
 left join bd_Customers c on c.ID = b.CustomerId
 left join bd_ShopSet d on d.id = b.ShopId 
 where SrcType = '1' and SendState = '0' and isnull(c.wxOpenID, '') <> ''
 union all  
 select b.PayDate as 消费时间, a.SrcDescript as 消费类型,SrcType, srcID,
 ISNULL(Cash, 0.00) + ISNULL(b.BankCard, 0.00) + ISNULL(UseCard, 0.00) as 消费金额,  
 0 as 卡扣次数, b.Remarks as 备注, c.Name as 会员姓名, d.Shop as 门店,c.wxOpenID, c.wxCode, a.id
 from wx_SendConsumeRecord a 
 left join SS_CustomerAddMoney b on a.srcid = b.MDID
 left join bd_Customers c on c.ID = b.CustomerId
 left join bd_ShopSet d on d.id = b.ShopId  
 where SrcType = '2' and SendState = '0' and isnull(c.wxOpenID, '') <> ''
 union all  
 select b.HandDate as 消费时间, a.SrcDescript as 消费类型,SrcType, srcID,
 ISNULL(Cash, 0.00) + ISNULL(b.BankCard, 0.00) + ISNULL(UseCard, 0.00) as 消费金额,  
 0 as 卡扣次数, b.FRemarks as 备注, c.Name as 会员姓名, d.Shop as 门店, c.wxOpenID, c.wxCode, a.id
 from wx_SendConsumeRecord a 
 left join HandBalance b on a.srcid = b.id
 left join bd_Customers c on c.ID = b.CustomerId
 left join bd_ShopSet d on d.id = b.ShopId  
 where SrcType = '3' and SendState = '0'   and isnull(c.wxOpenID, '') <> ''";
		
		
		$ob = M();
		$list = $ob->query(str_replace(
				array('消费类型','消费金额','卡扣次数','备注','消费时间','门店'), 
				array('stype','amount','count','note','time','store'), 
				$sql));
		
		foreach($list as $key=>$value){
			
			if(!empty($value['wxOpenID'])){
				$date = (array)($value['time']);
				$re = http(
					NotiTemplate::getUrl(),
					NotiTemplate::consume($value['wxOpenID'],
						C('WX_HTTP_URL').'/club/account/comment?id='.$value['id']."&srcID=".$value['srcID']."&SrcType=".$value['SrcType'],
							'您在'.$value['store'].'门店完成了一笔交易', substr($date['date'],0,10),$value['stype'],
								$value['amount'].'元',intval($value['count']).'次',$value['note'],'点击评价本次服务')
				);
				$ob->execute("update wx_SendConsumeRecord set SendState = '1' where ID = '{$value[id]}'");
// 				var_dump($re);
			}
		}
	}
	
}


class NotiTemplate {
	
	static function getUrl(){
		
		$ob = new AccessAction();
		$ACCESS_TOKEN = $ob->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;
		
		return $url ;
	}
	
	static function consume($OPENID,$URL,$first,
			$word1,$word2,$word3,$word4,$word5,$remark){
		
		$tpl['touser'] = $OPENID;
		$tpl['template_id'] = '7HY-zW8rAplxhc-syon5rajdw-ev0_849aSchqSpwdY';
		$tpl['url'] = $URL;
		$tpl['data'] = array(
			'first'		=> array('value'=>$first,'color'=>'#173177'),	
			'keyword1'	=> array('value'=>$word1,'color'=>'#173177'),	
			'keyword2'	=> array('value'=>$word2,'color'=>'#173177'),	
			'keyword3'	=> array('value'=>$word3,'color'=>'#173177'),	
			'keyword4'	=> array('value'=>$word4,'color'=>'#173177'),	
			'keyword5'	=> array('value'=>$word5,'color'=>'#173177'),	
			'remark'	=> array('value'=>$remark,'color'=>'#173177'),	
		);
		
		return json_encode($tpl);
	}
	
	
}